package com.example.springbook.service;

import com.example.springbook.enums.BookStatus;
import com.example.springbook.mapper.BookMapper;
import com.example.springbook.model.BookInfo;
import com.example.springbook.model.PageRequest;
import com.example.springbook.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        //获取总记录数
        Integer count = bookMapper.count();
        //获取当前页的记录
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        //处理状态
        // 0-删除 1-可借阅 2-不可借阅
        for(BookInfo bookInfo :bookInfos) {
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<BookInfo>(bookInfos, count, pageRequest);
    }

    public BookInfo queryBookInfoById(Integer id) {
        return bookMapper.queryBookById(id);
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer batchDeleteBookByIds(List<Integer> list) {
        return bookMapper.batchDeleteBookByIds(list);
    }
}
